/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ambimmort.ucagent.modules.server.channels;

import cn.net.greenet.db.bean.Messageoutgoinglog;
import cn.net.greenet.db.bean.MessageoutgoinglogManual;
import com.ambimmort.ucagent.modules.server.codec.UcRawMessage;
import com.ambimmort.ucagent.modules.server.dpi.DPIEndPoint;
import com.ambimmort.ucagent.modules.webservice.Cacher;
import com.ambimmort.ucagent.modules.webservice.Thing;
import com.ambimmort.ucagent.utils.HexUtil;
import com.ambimmort.ucagent.utils.IdUtils;
import org.apache.mina.core.session.IoSession;

import java.util.Date;

/**
 * @author 定巍
 */
public class ClientChannel {

    private ClientConnection connection = null;

    private DPIEndPoint dpiEndPoint = null;

    public ClientChannel(DPIEndPoint dpiEndPoint) {
        this.dpiEndPoint = dpiEndPoint;
        ClientChannelHandler handler = new ClientChannelHandler();
        connection = new ClientConnection(this.dpiEndPoint, dpiEndPoint.getDpiEndPointBean().getMasterIp(), dpiEndPoint.getDpiEndPointBean().getPort(), handler);
        connection.start();
    }

    public IoSession getSession() {
        return connection.getSession();
    }

    public boolean send_Manaul(UcRawMessage msg) {
        MessageoutgoinglogManual log = new MessageoutgoinglogManual();
        log.setDpiendpointbean(this.dpiEndPoint.getDpiEndPointBean());
        log.setMessageNo(msg.getMessageNo().toInteger());
        log.setMessageType(HexUtil.getHex(msg.getMessageType().toByte()));
        log.setMessageSequenceNo(msg.getMessageSequenceNo().toLong());
        log.setIsSentSuccess(false);

        log.setMessageSerialNo(-1);

        log.setSendError(null);
        log.setSendStartTime(new Date(System.currentTimeMillis()));
        log.setSendStopTime(new Date(System.currentTimeMillis()));
        log.setSendError("");
        log.setIsSentSuccess(true);
        log.setPacket(msg.getBytes());

        // updated
        log.setId(IdUtils.getId(log));

        boolean flag = false;
        if (isConnected()) {

            msg.setManaul(true);
            msg.setId(log.getId());
            msg.setLog(log);
            flag = connection.send(msg);
            if (flag == false) {
                log.setSendError("DPI拒绝接收");
            }
        } else {
            log.setSendError("DPI未上线");
            flag = false;
        }

        Thing thing = new Thing();
        thing.setData(log);
        thing.setAction(Thing.ADD);
        Cacher.getInstance().addTing(thing);
        return flag;
    }

    public boolean send_system(UcRawMessage msg) {
        if (isConnected()) {
            Messageoutgoinglog log = new Messageoutgoinglog();
            log.setDpiendpointbean(this.dpiEndPoint.getDpiEndPointBean());
            log.setMessageNo(msg.getMessageNo().toInteger());
            log.setMessageType(HexUtil.getHex(msg.getMessageType().toByte()));
            log.setMessageSequenceNo(msg.getMessageSequenceNo().toLong());
            log.setIsSentSuccess(false);
            log.setMessageSerialNo(-1);
            log.setSendError(null);
            log.setSendStartTime(new Date(System.currentTimeMillis()));
            log.setSendStopTime(new Date(System.currentTimeMillis()));
            log.setSendError("");
            log.setIsSentSuccess(true);
            log.setPacket(msg.getBytes());
// updated
            log.setId(IdUtils.getId(log));
            Thing thing = new Thing();
            thing.setData(log);
            thing.setAction(Thing.ADD);
            Cacher.getInstance().addTing(thing);
// updated end
            msg.setManaul(true);
            msg.setId(log.getId());
            boolean sendSuccess = connection.send(msg);
            return sendSuccess;
        } else {
            return false;
        }

    }

    public boolean send_no_log(UcRawMessage msg) {
        boolean sendSuccess = connection.send(msg);
        return sendSuccess;
    }

    public void stop() {
        if (connection != null) {
            connection.stopMe();
        }
    }

    public boolean isConnected() {
        if (connection == null) {
            return false;
        }
        return (connection.getSession() != null) && (connection.getSession().isConnected());
    }

}
